//
//  YZMPopupSheet.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/11.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMPopupSheet.h"

#define SHEETMINHEIGHT 90

@interface YZMPopupSheet()

@property (nonatomic, strong) UIView *contentHolderView;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, assign) BOOL hasShow;
@property (nonatomic, assign) CGFloat baseOffset;

@end

@implementation YZMPopupSheet

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame: frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.contentHolderView  = [[UIView alloc] init];
        [self addSubview: self.contentHolderView];
        self.contentHolderView.backgroundColor = [UIColor whiteColor];
        self.contentHolderView.layer.borderColor = [YZMAppStyle colorForLine].CGColor;
        [self.contentHolderView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.equalTo(self);
            make.height.mas_greaterThanOrEqualTo(SHEETMINHEIGHT);
            make.top.equalTo(self.mas_bottom);
        }];
        
        self.closeButton = [UIButton buttonWithType: UIButtonTypeCustom];
        [self.contentHolderView addSubview: self.closeButton];
        [self.closeButton setTitle:@"关闭" forState:UIControlStateNormal];
        [self.closeButton.titleLabel setFont:[YZMAppStyle mediumSizeFont]];
        [self.closeButton setTitleColor:[YZMAppStyle colorForRiseText] forState:UIControlStateNormal];
        [self.closeButton addTarget: self action: @selector(closeButtonPressed:) forControlEvents: UIControlEventTouchUpInside];
        [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentHolderView).with.offset(0);
            make.trailing.equalTo(self.contentHolderView).offset(-5);
            make.width.mas_equalTo(40);
            make.height.mas_equalTo(32);
        }];
        
        self.titleLabel = [YZMAppStyle labelWithFont:[YZMAppStyle mediumSizeBoldFont] andColor:[YZMAppStyle colorForGrayText]];
        [self.contentHolderView addSubview: self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.closeButton);
            make.centerX.equalTo(self.contentHolderView);
        }];
    }
    return self;
}

- (instancetype)initWithTitle: (NSString *)title
{
    self = [self initWithFrame: CGRectZero];
    if (self)
    {
        [self setTitle: title];
    }
    return self;
}


#pragma mark - operations

- (void)showInView: (UIView *)view
{
    [[NSNotificationCenter defaultCenter]  addObserver: self selector: @selector(keyboardWillShowNotice:) name: UIKeyboardWillShowNotification object: nil];
    [[NSNotificationCenter defaultCenter]  addObserver: self selector: @selector(keyboardWillHideNotice:) name: UIKeyboardWillHideNotification object: nil];
    
    [view addSubview: self];
    self.alpha = 0;
    self.frame = view.bounds;
    [self layoutIfNeeded];
    [self.contentHolderView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self);
        make.height.mas_greaterThanOrEqualTo(SHEETMINHEIGHT);
        make.bottom.equalTo(self).with.offset(-self.baseOffset);
        
    }];
    self.hasShow = YES;
    [UIView animateWithDuration: .5 animations:^{
        self.alpha = 1;
        [self layoutIfNeeded];
    }];
}

- (void)show
{
    UIView *view = YZMHCMInstance.rootTabBarCtrl.view;
    [self showInView: view];
}

- (void)dismiss
{
    [self dismissSelf];
}

- (void)dismissSelf
{
    self.alpha = 1;
    [self.contentHolderView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self);
        make.height.mas_greaterThanOrEqualTo(SHEETMINHEIGHT);
        make.top.equalTo(self.mas_bottom);
    }];
    self.hasShow = NO;
    [UIView animateWithDuration: .5f animations:^{
        self.alpha = 0.0;
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
    [[NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardWillShowNotification object: nil];
    [[NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardWillHideNotification object: nil];
}

- (void)setTitle: (NSString *)title
{
    self.titleLabel.text = title;
}


- (void)setContent: (UIView *)content
{
    [self setContent: content fullFill: YES];
}

- (void)setContent:(UIView *)content fullFill: (BOOL)full
{
    [self.contentHolderView addSubview: content];
    
    [content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.closeButton.mas_bottom);
        make.bottom.equalTo(self.contentHolderView);
        make.leading.trailing.equalTo(self);
    }];
}

+ (void)dismissForContentView: (UIView *)content
{
    YZMPopupSheet *sheet = (YZMPopupSheet *)content.superview.superview;
    if ([sheet isKindOfClass:[YZMPopupSheet class]]) {
        [sheet dismiss];
    }
}

#pragma mark - notice

- (void)keyboardWillShowNotice: (NSNotification *)notice
{
    NSDictionary *info = [notice userInfo];
    
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    self.baseOffset = keyboardSize.height;
    if (self.hasShow) {
        [self.contentHolderView mas_updateConstraints:^(MASConstraintMaker *make){
            make.bottom.equalTo(self).offset(-self.baseOffset);
        }];
    }
}


- (void)keyboardWillHideNotice: (NSNotification *)notice
{
    self.baseOffset = 0;
    if (self.hasShow) {
        [self.contentHolderView mas_updateConstraints:^(MASConstraintMaker *make){
            make.bottom.equalTo(self);
        }];
    }
}


#pragma mark - actions

- (void)dismissPress: (id)sender
{
    [self dismissSelf];
}

- (void)closeButtonPressed: (id)sender
{
    [self dismissSelf];
}


@end
